<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :align-center="true" width="50%" title="经销商银行卡">
    <el-form
      ref="dealerCardFormRef"
      :rules="rules"
      :disabled="dialogData.isView"
      :model="dialogData.itemInfo"
      label-width="100px"
      label-suffix=" :"
      :hide-required-asterisk="dialogData.isView"
    >
      <el-form-item label="卡号" prop="cardNo">
        <el-input v-model="dialogData.itemInfo.cardNo" maxlength="30" placeholder="请填写卡号" clearable></el-input>
      </el-form-item>
      <el-form-item label="开户人" prop="cardName">
        <el-input v-model="dialogData.itemInfo.cardName" maxlength="100" placeholder="请填写开户人" clearable></el-input>
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-input v-model="dialogData.itemInfo.bankName" maxlength="100" placeholder="请填写开户银行" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="dialogData.itemInfo.phone"
          maxlength="20"
          show-word-limit
          placeholder="请填写手机号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="phoneCode">
        <el-input v-model="dialogData.itemInfo.phoneCode" maxlength="10" placeholder="请填写验证码" clearable></el-input>
      </el-form-item>
      <el-form-item label="上传">
        <UploadImg
          v-model:imageUrl="dialogData.itemInfo.imgUrl"
          :disabled="dialogData.isView"
          :file-size="3"
          :upload-style="{ width: '130px', height: '130px' }"
          :api="uploadUserId"
        >
          <template #tip> 大小不能超过 2M </template>
        </UploadImg>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogData.isView" @click="handleSubmit" :loading="btnLoading">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import UploadImg from "@/components/Upload/Img.vue";
import { OptEnum } from "@/biz/eenum/appEnum";
import { DialogParam } from "@/biz/interface/DialogType";
import { DealerCardEntity } from "@/biz/interface/modules/DealerCardType";
import { getDealerCardInfoApi, saveDealerCardApi } from "@/api/modules/dealer/card";
import { uploadUserId } from "@/api/modules/upload";
//页面的数据
interface dealerApplyDialogOption extends DialogParam {
  //弹出表单的标题
  title: string;
  //是否是查看状态，查看状态是不可以提交数据
  isView: boolean;
  //当前的数据对象
  itemInfo: DealerCardEntity;
  //其它的数据都继续在这里加
}
//提交按钮，避免多次提交
const btnLoading = ref(false);
//弹出框的参数
const dialogVisible = ref(false);
//当前表单数据在这里初始化
const dialogData = reactive<dealerApplyDialogOption>({
  optType: OptEnum.UNKNOW,
  title: "",
  isView: false,
  itemInfo: {
    cardNo: "",
    cardName: "",
    bankName: "",
    phone: "",
    phoneCode: "",
    imgUrl: ""
  }
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogParam): void => {
  dialogData.optType = params.optType;
  dialogData.rowData = params.rowData;
  dialogData.isView = dialogData.optType === OptEnum.VIEW;

  if (dialogData.optType === OptEnum.CREATE) {
    initItemInfo();
  } else if (dialogData.rowData && dialogData.rowData.id) {
    getItemInfo(dialogData.rowData.id);
  }

  dialogVisible.value = true;
};

//初始化页面的数据
const initItemInfo = (): void => {
  dialogData.itemInfo = {
    cardNo: "",
    cardName: "",
    bankName: "",
    phone: "",
    phoneCode: "",
    imgUrl: ""
  };
};
//获取当前数据的详情，直接调用接口获取数据
const getItemInfo = async (id: string) => {
  if (id) {
    let { data } = await getDealerCardInfoApi({ id });
    dialogData.itemInfo = data as unknown as DealerCardEntity;
  }
};

//数据较验
const rules = reactive({
  cardNo: [{ required: true, message: "请填写卡号", trigger: "blur" }],
  cardName: [{ required: true, message: "请填写开户人", trigger: "blur" }],
  bankName: [{ required: true, message: "请填写开户银行", trigger: "blur" }],
  phone: [{ required: true, message: "请填写手机号", trigger: "blur" }],
  phoneCode: [{ required: true, message: "请填写验证码", trigger: "blur" }]
});

// 提交数据（新增/编辑）
const dealerCardFormRef = ref<FormInstance>();
const handleSubmit = () => {
  btnLoading.value = true;
  dealerCardFormRef.value!.validate(async valid => {
    if (!valid) {
      btnLoading.value = false;
      return;
    }
    try {
      if (dialogData.optType === OptEnum.CREATE || dialogData.optType === OptEnum.EDIT) {
        //提交数据
        let res = await saveDealerCardApi({
          ...dialogData.itemInfo
        });
        if (res.data) {
          ElMessage.success({ message: `${dialogData.itemInfo.cardNo}绑定成功！` });
          //刷新列表数据
          emits("refresh-list");
          dialogVisible.value = false;
        }
      }
    } catch (error) {
    } finally {
      btnLoading.value = false;
    }
  });
};

const emits = defineEmits(["refresh-list"]);

//页面参数
defineExpose({
  acceptParams
});
</script>
